/**
 * 
 */
package org.universalmvc.config;

import java.util.List;

import com.thoughtworks.xstream.XStream;

/**
 * @author vj
 *
 */
public class ConfigurationProcessor {
	private static final XStream xstream = new XStream();
	
	static {
		xstream.processAnnotations(Application.class);
	}
	
	public static final Configuration read(final ConfigurationSource cs) {
		return new Configuration((Application) xstream.fromXML(cs.getInputStream()));
	}
	
	public static final Configuration read(final List<ConfigurationSource> configSources) {
		// atleast, return an empty configuration by default
		Configuration result = new Configuration();
		
		if((configSources != null) && (!configSources.isEmpty())){
			for(ConfigurationSource cs: configSources) {
				result.merge(ConfigurationProcessor.read(cs));
			}
		}
		
		return result;
	}
	
	public static final String toXml(Configuration configuration) {
		return xstream.toXML(configuration.getApplication());
	}
}
